/**
 * Author: Shen Yanqiu
 * Date: 2017-12-15
 * Time: 15:04
 *
 */
import modelExtend from 'dva-model-extend';
import { routerRedux } from 'dva/router';
import { token, routerTool } from 'utils';
import { pageModel } from '../common';
import { getExpenditureList } from './../../services/expenditure';
import { message } from 'antd';
import {getActivityDetails} from "../../services/activity";

export default modelExtend (pageModel, {
  namespace: 'examine',

  state: {

  },

  subscriptions: {
    setup ({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === routerTool.getPathByName('Examine')) {
          dispatch({ type: 'getWorknoticeList' });
        }
      });
    },
  },

  effects: {
    // 获取列表
    * getWorknoticeList ({ payload = {} }, { call, put }) {
      // console.log(payload);
      const { success, data } = yield call(getExpenditureList, {
        page: payload.current,
        pageSize: payload.pageSize,
        name: payload.name || undefined,
        dept: payload.dept || undefined,
      });
      if (success && data) {
        yield put({
          type: 'loadSuccess',
          payload: {
            list: data.list,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.total,
            },
          },
        });
      }
    },
    * onSeeItemModal ({ payload: id }, { put, call}) {
      console.log('see'+ id.id);
      yield put(
        routerTool.redirectTo({
          name: 'ExamineActivityDetails',
          params: {
            id: id.id,
          },
        })
      );
    },
    * onDeleteItemModal ({ payload: {params} }) {
      console.log('delete'+ params.id);
    },
    * searchData({ payload: formData }) {
      console.log(formData);
    },
    * submitExamine ({ payload: { values } }) {
      console.log(values);
    },
    * refreshDetails ({ payload: id }, { call, put }) {
      const { data } = yield call(getActivityDetails, id);
      yield put({ type: 'update', payload: { data } });
    },
  },

  reducers: {
    update (state, { payload: { data: detailsInfo } }) {
      return { ...state, detailsInfo };
    },
  },
});
